package com.nwpu.main;

import com.nwpu.main.dao.JDBCUtil;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * MethodSetImpl
 *
 * @author 龙虹宇
 * @version 1.0
 * @date 2023/11/1
 */
public class MethodSetImpl extends UnicastRemoteObject implements IMethodSet {
    // 默认端口
    private int port;
    // 默认IP
    private String ip;
    // 指令集
    private static final String[] INSTRUCTIONS = {
            "register", "login", "add"
            , "query", "delete", "clear"
            , "help", "quit"
    };
    // 指令集描述
    private static final String[] INSTRUCTIONS_DES = {
            "注册用户", "用户登录", "添加会议"
            , "查询会议", "删除会议", "清除会议"
            , "查看帮助", "退出"
    };
    // 指令集参数
    private static final String[] INSTRUCTIONS_ARGS = {
            "[username] [password]"
            , "[username] [password]"
            , "[username] [password] [otherusername] [start] [end] [title]"
            , "[username] [password] [start] [end]"
            , "[username] [password] [meetingid]"
            , "[username] [password]"
            , "no args", "no args"
    };

    /**
     * 无参构造
     *
     * @throws RemoteException
     */
    protected MethodSetImpl(int port, String ip) throws RemoteException {
        this.port = port;
        this.ip = ip;
    }

    /**
     * 输出帮助信息
     *
     * @return
     */
    public String helpInfo() {
        String tab = "\t";
        String lf = "\n";
        String info = "RMI MENU:" + lf;
        for (int i = 0; i < 8; i++) {
            info += tab + (i + 1) + ". " + INSTRUCTIONS[i]
                    + tab + " : " + INSTRUCTIONS_DES[i] + lf;
            info += tab + tab + "arguments: " + INSTRUCTIONS_ARGS[i] + lf;
        }
        return info;
    }

    /**
     * 处理输入的指令
     *
     * @param instruction
     * @return
     * @throws RemoteException
     */
    @Override
    public String processInstruction(String instruction) throws RemoteException {
        // 判断指令合法性
        String[] strings = instruction.split(" ");
        String ins = strings[4];
        // 当前方法的返回信息
        String wrongIns = "error：您输入的指令有误，请重新输入!";
        String returnStr = "";
        // 调用远程方法后的返回信息
        String methodStr;
        switch (ins) {
            case "register":
                if (strings.length != 7) return wrongIns;
                methodStr = register(strings[5], strings[6]);
                if ("success".equals(methodStr)) returnStr = "注册成功，欢迎您：" + strings[5];
                else if ("failure".equals(methodStr)) returnStr = "error：注册失败！";
                else if ("existed".equals(methodStr)) returnStr = "error：用户名已存在，请重新注册！";
                break;
            case "login":
                if (strings.length != 7) return wrongIns;
                methodStr = login(strings[5], strings[6]);
                if ("success".equals(methodStr)) returnStr = "登录成功，欢迎您：" + strings[5];
                else if ("failure".equals(methodStr)) returnStr = "error：登录失败！";
                else if ("wrong".equals(methodStr)) returnStr = "error：用户名或密码错误，请重新输入！";
                break;
            case "add":
                if (strings.length != 13) return wrongIns;
                methodStr = add(strings[5], strings[6], strings[7], strings[8]+ " " + strings[9], strings[10] + " " + strings[11], strings[12]);
                if ("success".equals(methodStr)) returnStr = "“" + strings[12] + "”会议创建成功";
                else if ("failure".equals(methodStr)) returnStr = "error：会议创建失败";
                else if ("wrong".equals(methodStr)) returnStr = "error：用户名或密码或参会者用户名错误";
                else if ("timeOverlap".equals(methodStr)) returnStr = "error：会议时间冲突，请重新创建";
                else if ("wrongTime".equals(methodStr)) returnStr = "error：时间格式错误，请重新输入";
                else if ("wrongTimeValue".equals(methodStr)) returnStr = "error：结束时间大于开始时间";
                break;
            case "query":
                if (strings.length != 11) return wrongIns;
                methodStr = query(strings[5], strings[6], strings[7] + " " + strings[8], strings[9] + " " + strings[10]);
                if (methodStr.startsWith("==Meetings==")) returnStr = methodStr;
                else if ("wrong".equals(methodStr)) returnStr = "error：用户名或密码错误，请重新输入";
                else if ("none".equals(methodStr)) returnStr = "info：未查询到相应会议";
                else if ("wrongTime".equals(methodStr)) returnStr = "error：时间格式错误，请重新输入";
                else if ("wrongTimeValue".equals(methodStr)) returnStr = "error：结束时间大于开始时间";
                break;
            case "delete":
                if (strings.length != 8) return wrongIns;
                methodStr = delete(strings[5], strings[6], Integer.parseInt(strings[7]));
                if ("success".equals(methodStr)) returnStr = "会议删除成功";
                else if ("wrong".equals(methodStr)) returnStr = "error：用户名或密码错误，请重新输入";
                else if ("none".equals(methodStr)) returnStr = "error：未查询到相应会议";
                else if ("failure".equals(methodStr)) returnStr = "error：会议删除失败";
                break;
            case "clear":
                if (strings.length != 7) return wrongIns;
                methodStr = clear(strings[5], strings[6]);
                if (methodStr.startsWith("success")) returnStr = methodStr;
                else if ("wrong".equals(methodStr)) returnStr = "error：用户名或密码错误，请重新输入";
                else if ("none".equals(methodStr)) returnStr = "error：未查询到相应会议";
                else if ("failure".equals(methodStr)) returnStr = "error：会议删除失败";
                break;
            case "help":
                if (strings.length != 5) return wrongIns;
                returnStr = helpInfo();
                break;
            case "quit":
                returnStr = "quit";
                break;
            default:
                System.out.println("error：指令输入错误");
                return wrongIns;
        }
        return returnStr;
    }

    /**
     * 注册
     *
     * @param username
     * @param password
     * @return
     * @throws RemoteException
     */
    @Override
    public String register(String username, String password) throws RemoteException {
        return JDBCUtil.register(username, password);
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     * @throws RemoteException
     */
    @Override
    public String login(String username, String password) throws RemoteException {
        return JDBCUtil.login(username, password);
    }

    /**
     * 创建会议
     *
     * @param username
     * @param password
     * @param otherUsername
     * @param start
     * @param end
     * @param title
     * @return
     * @throws RemoteException
     */
    @Override
    public String add(String username, String password, String otherUsername, String start, String end, String title) throws RemoteException {
        return JDBCUtil.add(username, password, otherUsername, start, end, title);
    }

    /**
     * 查询会议
     *
     * @param username
     * @param password
     * @param start
     * @param end
     * @return
     * @throws RemoteException
     */
    @Override
    public String query(String username, String password, String start, String end) throws RemoteException {
        return JDBCUtil.query(username, password, start, end);
    }

    /**
     * 删除会议
     *
     * @param username
     * @param password
     * @param meetingId
     * @return
     * @throws RemoteException
     */
    @Override
    public String delete(String username, String password, int meetingId) throws RemoteException {
        return JDBCUtil.delete(username, password, meetingId);
    }

    /**
     * 删除用户创建的所有会议
     *
     * @param username
     * @param password
     * @return
     * @throws RemoteException
     */
    @Override
    public String clear(String username, String password) throws RemoteException {
        return JDBCUtil.clear(username, password);
    }

    /**
     * 关闭资源
     *
     * @throws RemoteException
     */
    @Override
    public void close() throws RemoteException {
        JDBCUtil.close();
    }
}
